package com.jobinn.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jobinn.DAO.JobInfoDAO;
import com.jobinn.DAO.JobOpportunityDAO;
import com.jobinn.JDOBean.JobInfo;
import com.jobinn.JDOBean.JobOpportunity;
import com.jobinn.service.UserService;
import com.jobinn.web.actionForm.AddJobForm;

public class AddJobAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
				this.getServlet().getServletContext()); 
		JobOpportunityDAO jobOpportunityDAO =(JobOpportunityDAO)ctx.getBean("jobOpportunityDAO");
		JobInfoDAO jobInfoDAO =(JobInfoDAO)ctx.getBean("jobInfoDAO");
		
		String url = "";
		String content = "";
		String jobTitle = "";
		
		AddJobForm addJobForm = (AddJobForm)form;
		
		url = addJobForm.getUrl();
		content = addJobForm.getContent();
		jobTitle = addJobForm.getJobTitle();
		
		if(url != null && content != null && jobTitle != null && !"".equals(url) && !"".equals(content) && !"".equals(jobTitle)){
			JobOpportunity jobOpportunity = new JobOpportunity();
			JobInfo jobInfo = new JobInfo();
			
			jobOpportunity.setUrl(url);
			jobOpportunity.setStatus(new Integer(2));
			
			jobInfo.setContent(content);
			jobInfo.setTitle(jobTitle);
			
			jobOpportunity.setJobInfo(jobInfo);
			jobInfo.setJobOpportunity(jobOpportunity);
			
			try{
				jobOpportunityDAO.save(jobOpportunity);
				jobInfoDAO.save(jobInfo);
			}catch(Exception ex){
				ex.printStackTrace();
			}	
		}
		
		return mapping.findForward("addjob");
	}
	
}
